package com.ruoyi.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.AdminConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.AdminWeekly;
import com.ruoyi.system.service.IAdminWeeklyService;
import com.ruoyi.web.vo.AdminWeeklyStatisticView;
import com.ruoyi.web.vo.AdminWeeklyView;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 周报Controller
 * 
 * @author ruoyi
 * @date 2021-01-20
 */
@Controller
@RequestMapping("/system/weekly")
public class AdminWeeklyController extends BaseController
{
    private String prefix = "system/weekly";

    @Autowired
    private IAdminWeeklyService adminWeeklyService;

    @RequiresPermissions("system:weekly:view")
    @GetMapping()
    public String weekly(ModelMap mmap)
    {
        Date lastMonday = DateUtils.getLastMonday();
        Date thisMonday = DateUtils.getThisMonday();
        Date lastFriday = DateUtils.getLastFriday();
        Date thisFriday = DateUtils.getThisFriday();
        mmap.put("thisMonday", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, thisMonday));
        mmap.put("lastMonday", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, lastMonday));
        mmap.put("thisFriday", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, thisFriday));
        mmap.put("lastFriday", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, lastFriday));
        SysUser currentUser = ShiroUtils.getSysUser();
        mmap.put("currentUserId", currentUser.getUserId());
        return prefix + "/weekly";
    }
    /**
     * 查询周五日期
     */
    @GetMapping("/friday/{startDate}")
    @ResponseBody
    public AjaxResult getProductData(@PathVariable("startDate") String startDate)
    {
        String endDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.getWeekFriday(DateUtils.parseDate(startDate)));
        AjaxResult ajax = AjaxResult.success(JSONArray.toJSON(endDate));
        return ajax;
    }

    @GetMapping("/statistics")
    public String statistics(ModelMap mmap)
    {
        Date lastMonday = DateUtils.getLastMonday();
        Date lastFriday = DateUtils.getLastFriday();
        mmap.put("lastMonday", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, lastMonday));
        mmap.put("lastFriday", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, lastFriday));

        return prefix + "/statistics";
    }

    @GetMapping("/statistics/weeklyDateStart/{weeklyDateStart}/weeklyDateEnd/{weeklyDateEnd}/deptId/{deptId}/productId/{productId}/productType/{productType}")
    public String statisticsInfo(@PathVariable("weeklyDateStart") String weeklyDateStart,
                                 @PathVariable("weeklyDateEnd") String weeklyDateEnd,@PathVariable("deptId") String deptId,
                                 @PathVariable("productId") String productId,@PathVariable("productType") String productType, ModelMap mmap)
    {
        AdminWeeklyView adminWeekly = new AdminWeeklyView();
        adminWeekly.setStartDate(DateUtils.parseDate(weeklyDateStart));
        adminWeekly.setEndDate(DateUtils.parseDate(weeklyDateEnd));
        adminWeekly.setDeptId(Long.parseLong(deptId));
        adminWeekly.setProductId(Long.parseLong(productId));
        adminWeekly.setProductType(Long.parseLong(productType));
        List<AdminWeeklyStatisticView> list = adminWeeklyService.selectAdminWeeklyStatisticsList(adminWeekly);
        mmap.put("adminWeekly", list.get(0));
        return prefix + "/statisticsInfo";
    }

    /**
     * 查询周报列表
     */
    @RequiresPermissions("system:weekly:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AdminWeeklyView adminWeekly)
    {
        startPage();
        List<AdminWeeklyView> list = adminWeeklyService.selectAdminWeeklyList(adminWeekly);
        return getDataTable(list);
    }

    /**
     * 导出周报列表
     */
    @RequiresPermissions("system:weekly:export")
    @Log(title = "周报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AdminWeeklyView adminWeekly)
    {
        List<AdminWeeklyView> list = adminWeeklyService.selectAdminWeeklyList(adminWeekly);
        ExcelUtil<AdminWeeklyView> util = new ExcelUtil<AdminWeeklyView>(AdminWeeklyView.class);
        return util.exportExcel(list, "weekly");
    }/**
     * 查询周报统计列表
     */
    @RequiresPermissions("system:weekly:list")
    @PostMapping("/statistics")
    @ResponseBody
    public TableDataInfo statistics(AdminWeeklyView adminWeekly)
    {
        startPage();
        List<AdminWeeklyStatisticView> list = adminWeeklyService.selectAdminWeeklyStatisticsList(adminWeekly);
        return getDataTable(list);
    }

    /**
     * 导出周报统计列表
     */
    @RequiresPermissions("system:weekly:export")
    @Log(title = "周报统计", businessType = BusinessType.EXPORT)
    @PostMapping("/exportStatistics")
    @ResponseBody
    public AjaxResult exportStatistics(AdminWeeklyView adminWeekly)
    {
        List<AdminWeeklyView> list = adminWeeklyService.selectAdminWeeklyList(adminWeekly);
        ExcelUtil<AdminWeeklyView> util = new ExcelUtil<AdminWeeklyView>(AdminWeeklyView.class);
        return util.exportExcel(list, "weekly");
    }

    /**
     * 新增周报
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        Date thisMonday = DateUtils.getThisMonday();
        Date thisFriday = DateUtils.getThisFriday();
        mmap.put("thisMonday", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, thisMonday));
        mmap.put("thisFriday", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, thisFriday));
        return prefix + "/add";
    }
    /**
     * 新增周报任务
     */
    @GetMapping("/addTask/{num}")
    public String addTask(@PathVariable("num") Long num,ModelMap mmap)
    {
        mmap.put("num", num);
        return prefix + "/job";
    }

    /**
     * 新增保存周报
     */
    @RequiresPermissions("system:weekly:add")
    @Log(title = "周报", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AdminWeekly adminWeekly)
    {
        Long userId = ShiroUtils.getUserId();
        adminWeekly.setUserId(userId);
        MultipartFile file = adminWeekly.getAttachmentFile();
        // 上传文件路径
        String filePath = AdminConfig.getUploadPath();
        // 上传并返回新文件名称
        try {
            String path = FileUploadUtils.upload(filePath, file);
            adminWeekly.setAttachmentPath(path);
            adminWeekly.setAttachmentName(file.getOriginalFilename());
        } catch (IOException e) {
            return AjaxResult.error(e.getMessage());
        }
        String msg = adminWeeklyService.insertAdminWeekly(adminWeekly);
        if (StringUtils.isNotEmpty(msg)) {
            return error(msg);
        }
        return success();
    }

    /**
     * 查看周报
     */
    @GetMapping("/detail/{weeklyId}")
    public String detail(@PathVariable("weeklyId") Long weeklyId, ModelMap mmap)
    {
        AdminWeekly adminWeekly = adminWeeklyService.selectAdminWeeklyById(weeklyId);
        mmap.put("adminWeekly", adminWeekly);
        return prefix + "/detail";
    }

    /**
     * 修改周报
     */
    @GetMapping("/edit/{weeklyId}")
    public String edit(@PathVariable("weeklyId") Long weeklyId, ModelMap mmap)
    {
        AdminWeekly adminWeekly = adminWeeklyService.selectAdminWeeklyById(weeklyId);
        mmap.put("adminWeekly", adminWeekly);
        return prefix + "/edit";
    }

    /**
     * 修改保存周报
     */
    @RequiresPermissions("system:weekly:edit")
    @Log(title = "周报", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AdminWeekly adminWeekly)
    {
        MultipartFile file = adminWeekly.getAttachmentFile();
        // 上传文件路径
        String filePath = AdminConfig.getUploadPath();
        // 上传并返回新文件名称
        try {
            String path = FileUploadUtils.upload(filePath, file);
            adminWeekly.setAttachmentPath(path);
            adminWeekly.setAttachmentName(file.getOriginalFilename());
        } catch (IOException e) {
            return AjaxResult.error(e.getMessage());
        }
        return toAjax(adminWeeklyService.updateAdminWeekly(adminWeekly));
    }

    /**
     * 删除周报
     */
    @RequiresPermissions("system:weekly:remove")
    @Log(title = "周报", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(adminWeeklyService.deleteAdminWeeklyByIds(ids));
    }
}
